<template>
  <div class="app-container">
    <div class="search-container">
      <el-form v-show="showSearch" ref="queryParamsRef" :inline="true" :model="queryParams" label-width="90px">
        <el-form-item v-if="!userInfo.orgId" label="所属机构">
          <select-org-tree :value="queryParams.orgId" @orgNodeClick="orgNodeClick"/>
        </el-form-item>
        <el-form-item label="出入库类型" prop="inoutType">
          <el-select v-model="queryParams.inoutType" class="width-120" clearable placeholder="请选择出入库类型">
            <el-option v-for="dict in dictDataMap['in_out_distinguish']" :key="dict.dictValue"
                       :label="dict.dictLabel" :value="dict.dictValue"/>
          </el-select>
        </el-form-item>
        <el-form-item label="出入库方式" prop="type">
          <el-select v-model="queryParams.type" class="width-120" clearable placeholder="请选择出入库方式">
            <el-option v-for="dict in dictDataMap['material_storage_type']" :key="dict.dictValue"
                       :label="dict.dictLabel" :value="dict.dictValue"/>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="所属类别" prop="statisticClass">
            <el-input v-model="queryParams.statisticClass" placeholder="请输入所属类别" clearable
                @keyup.enter.native="handleQuery" />
        </el-form-item> -->
        <el-form-item label="库房类型" prop="storageType">
          <el-select v-model="queryParams.storageType" class="width-120" clearable placeholder="请选择库房类型">
            <el-option v-for="dict in dictDataMap['material_storage_dept_type']" :key="dict.dictValue"
                       :label="dict.dictLabel" :value="dict.dictValue"/>
          </el-select>
        </el-form-item>
        <el-form-item label="来源\去向" prop="fromLevel">
          <el-select v-model="queryParams.fromLevel" class="width-120" clearable placeholder="请选择来源">
            <el-option v-for="dict in dictDataMap['material_from_level']" :key="dict.dictValue"
                       :label="dict.dictLabel" :value="dict.dictValue"/>
          </el-select>
        </el-form-item>
        <el-form-item label="是否记账" prop="accountFlag">
          <el-select v-model="queryParams.accountFlag" class="width-120" clearable placeholder="请选择是否记账">
            <el-option v-for="dict in dictDataMap['sys_yes_no']" :key="dict.dictValue"
                       :label="dict.dictLabel" :value="dict.dictValue"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <query-form-button @handleQuery="handleQuery" @resetQuery="resetQuery"/>
        </el-form-item>
      </el-form>
    </div>
    <el-row :gutter="10" class="mb8" style="margin-right: 180px">
      <el-col :span="1.5">
        <el-button v-hasPerm="['base:materialInoutClass:add']" plaintype="primary" @click="handleAdd">
          <template
            #icon>
            <i-ep-plus/>
          </template>
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPerm="['base:materialInoutClass:export']" plain type="warning"
                   @click="handleExport">
          <template #icon>
            <i-ep-download/>
          </template>
          导出
        </el-button>
      </el-col>
    </el-row>

    <el-table v-adaptive :border="true" :data="materialInoutClassList" :header-cell-style="$headerCellStyle" :stripe="true"
              highlight-current-row>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="库房类型" prop="storageType">
        <template #default="scope">
          <dict-data-tag :options="dictDataMap['material_storage_dept_type']" :value="scope.row.storageType"/>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="出入库类型" prop="inoutType">
        <template #default="scope">
          <dict-data-tag :options="dictDataMap['in_out_distinguish']" :value="scope.row.inoutType"/>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="出入库方式" prop="type">
        <template #default="scope">
          <dict-data-tag :options="dictDataMap['material_storage_type']" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <!--            <el-table-column :show-overflow-tooltip="true" label="所属类别" :align="'center'" prop="statisticClass"/>-->

      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="来源\去向" prop="fromLevel">
        <template #default="scope">
          <dict-data-tag :options="dictDataMap['material_from_level']" :value="scope.row.fromLevel"/>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="是否记账" prop="accountFlag">
        <template #default="scope">
          <dict-data-tag :options="dictDataMap['sys_yes_no']" :value="scope.row.accountFlag"/>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="排序" prop="sort"/>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="备注信息" prop="remarks"/>
      <el-table-column :align="'center'" class-name="small-padding fixed-width" label="操作">
        <template #default="scope">
          <el-button v-hasPerm="['base:materialInoutClass:edit']" type="success"
                     @click="handleUpdate(scope.row)">
            <template #icon>
              <i-ep-edit/>
            </template>
            修改
          </el-button>
          <el-button v-hasPerm="['base:materialInoutClass:remove']" type="danger"
                     @click="handleDelete(scope.row)">
            <template #icon>
              <i-ep-delete/>
            </template>
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNum"
                :total="total" @pagination="getList"/>

    <!-- 添加或修改出出入库类型字典表对话框 -->
    <el-dialog v-model="open" :close-on-click-modal="false" :title="title" append-to-body width="400px">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="40">
          <el-col :span="24">
            <el-form-item label="库房类型" prop="storageType">
              <el-select v-model="form.storageType" placeholder="请选择库房类型" style="width: 100%;">
                <el-option v-for="dict in dictDataMap['material_storage_dept_type']" :key="dict.dictValue"
                           :label="dict.dictLabel" :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="出入库类型" prop="inoutType">
              <el-radio-group v-model="form.inoutType">
                <el-radio v-for="dict in dictDataMap['in_out_distinguish']" :key="dict.dictValue"
                          :value="dict.dictValue">{{ dict.dictLabel }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="出入库方式" prop="type">
              <el-select v-model="form.type" placeholder="请选择出入库方式" style="width: 100%">
                <el-option v-for="dict in dictDataMap['material_storage_type']" :key="dict.dictValue"
                           :label="dict.dictLabel" :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col> 
          <el-col :span="24">
            <el-form-item label="来源\去向" prop="fromLevel">
              <el-select v-model="form.fromLevel" placeholder="请选择来源" style="width: 100%">
                <el-option v-for="dict in dictDataMap['material_from_level']" :key="dict.dictValue"
                           :label="dict.dictLabel" :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="是否记账">
              <el-radio-group v-model="form.accountFlag">
                <el-radio v-for="dict in dictDataMap['sys_yes_no']" :key="dict.dictValue"
                          :value="dict.dictValue">{{
                    dict.dictLabel
                  }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="排序" prop="sort">
              <el-input v-model="form.sort" placeholder="请输入排序" type="number"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注信息" prop="remarks">
              <el-input v-model="form.remarks" placeholder="请输入备注信息"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
                <span class="dialog-footer">
                    <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import {
  addMaterialInoutClass,
  delMaterialInoutClass,
  getMaterialInoutClass,
  selectPageMaterialInoutClass,
  updateMaterialInoutClass
} from "@/api/material/materialInoutClass";

import {useUserStoreHook} from "@/store/modules/user";
import {useDictStoreHook} from "@/store/modules/dict";

const formRef = ref()
const queryParamsRef = ref()
const useUserStore = useUserStoreHook();
const {proxy} = getCurrentInstance() as any
const dictStore = useDictStoreHook();
const dictDataMap = ref(dictStore.isTypeGetData('material_from_level', 'sys_yes_no', 'material_storage_type', 'in_out_distinguish', 'material_storage_dept_type'))
// 按钮loading
const buttonLoading = ref(false)
// 登录用户信息
const userInfo = <UserInfoBusi>useUserStore.user.userInfo
// 选中数组
const ids = ref([])
// 非单个禁用
const single = ref(true)
// 非多个禁用
const multiple = ref(true)
// 显示搜索条件
const showSearch = ref(true)
// 总条数
const total = ref(0)
// 出入库方式字典表表格数据
const materialInoutClassList = ref([])
// 弹出层标题
const title = ref("")
// 是否显示弹出层
const open = ref(false)
// 查询参数
const queryParams = ref<any>({
  pageNum: 1,
  pageSize: 10,
  inoutType: undefined,
  type: undefined, 
  storageType: undefined,
  fromLevel: undefined,
  accountFlag: undefined,
})
// 表单参数
const form = ref<any>([])
// 表单校验
const rules = ref({
  storageType: [
    {required: true, message: "库房类型不能为空", trigger: "change"}
  ],
  inoutType: [
    {required: true, message: "出入库区分不能为空", trigger: "change"}
  ],
  type: [
    {required: true, message: "出入库方式不能为空", trigger: "change"}
  ],
  fromLevel: [
    {required: true, message: "来源不能为空", trigger: "change"}
  ],
  sort: [
    {required: true, message: "排序不能为空", trigger: "blur"}
  ],
})

if (userInfo.orgId) {
  queryParams.value.orgId = userInfo.orgId 
  getList()
}

// 机构树点击事件
function orgNodeClick(data, _e) {
  queryParams.value.orgId = data.orgId
  if (queryParams.value.orgId) {
    getList()
  }
}

/** 查询出出入库方式字典表列表 */
function getList() {
  if (!queryParams.value.orgId) {
    return ElMessage.error(proxy.$OrgIdIsEmpty)
  }
  selectPageMaterialInoutClass(queryParams.value).then((response: any) => {
    materialInoutClassList.value = response.rows;
    total.value = response.total;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    inoutClassId: undefined,
    inoutType: "1",
    type: undefined, 
    storageType: undefined,
    fromLevel: undefined,
    accountFlag: "Y",
    orgId: queryParams.value.orgId,
    orgName: userInfo.orgName,
    delFlag: undefined,
    createBy: undefined,
    createTime: undefined,
    updateBy: undefined,
    updateTime: undefined,
    remarks: undefined,
    sort: undefined
  };
  nextTick(() => {
    formRef.value.resetFields()
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryParamsRef.value.resetFields()
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.inoutClassId)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  if (!queryParams.value.orgId) {
    return ElMessage.error(proxy.$OrgIdIsEmpty)
  }
  open.value = true;
  reset();
  title.value = "添加出入库类型字典表";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const id = row.inoutClassId || ids.value
  getMaterialInoutClass(id).then((response: any) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改出入库类型字典表";
  });
}

/** 提交按钮 */
function submitForm() {
  (formRef.value as any).validate(valid => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.inoutClassId != null) {
        updateMaterialInoutClass(form.value).then(() => {
          ElMessage.success("修改成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      } else {
        addMaterialInoutClass(form.value).then(() => {
          ElMessage.success("新增成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  debugger
  const idsThis = row.inoutClassId || ids.value;
  ElMessageBox.confirm('是否确认删除出入库分类字典表数据？',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    delMaterialInoutClass(idsThis).then(() => {
      getList();
      ElMessage.success("删除成功");
    })
  }).catch(() => {
  }).finally(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  // download('base/drugInoutClass/export', {
  //     ...queryParams
  // }, `drugInoutClass_${new Date().getTime()}.xlsx`)
}
</script>

<script lang="ts">
export default {
  name: "MaterialInoutClass"
}
</script>

<style lang="scss" scoped></style>
